<?php
/*
 *wengcanvip@qq.com  1st April 2012
 */
if (!defined('CURRENT_PATH')){
	die('Hacking attempt');
	}
if($_REQUEST['act']&&$_REQUEST['act']=='saveAction'){	
/*-------------------------------------------------------------*/	
	$ps=spClass(product_summary);
	$productId=0;
if($this->spArgs(productId)!=null){
	$productId=intval($this->spArgs(productId));
	}			
/*------- 2012.04.01-------------------------------------*/
		$showOrder=intval($this->spArgs('show_order'));
		$showOrder=$showOrder>=0?$showOrder:0;//set the minimum  show order of a post
		$showOrder=$showOrder<=9999?$showOrder:9999;//set the maximum  show order of a post
		$saveMapArr=array('products_fid'=>'fid','name'=>'product_name','thumb'=>'product_thumb');
		if($productId!=0){
			$row=$this->DataToSave($saveMapArr,$this->spArgs(),array('product_add_author'=>$_SESSION['adminUserName'],'product_add_time'=>time(),'show_order'=>$showOrder,'product_status'=>1));
			$ps->update($conditions=array('id'=>$productId),$row);	
			header("Content-Type:text/html;charset=utf-8");
			echo'<script> alert("恭喜，产品信息修改成功！");parent.location.reload();</script>';
		}else{
			$newrow=$this->DataToSave($saveMapArr,$this->spArgs(),array(id=>null,'product_add_author'=>$_SESSION['adminUserName'],'product_add_time'=>time(),'show_order'=>$showOrder));
			$ps->create($newrow);
			$this->success('恭喜！产品添加成功！');
		}	
		unset($ps);
/*-------------------------------------------------------*/	
	
			
}else if($_REQUEST['act']&&$_REQUEST['act']=='editframe'){//显示iframe
	$this->id=intval($_REQUEST['id']);
	$this->display('management/products_frame.html');
}else if($_REQUEST['act']&&$_REQUEST['act']=='modify'){
	$id=intval($_REQUEST['id']);
	$ps=spClass(product_summary);
	$res=$ps->find($conditions=array(id=>$id),$sort = null, $fields =null);
	if(!empty($res[mid])){
		$ms=spClass(site_meta);
		$metares=$ms->find($conditions=array(umeta_id=>$res[mid]));
		$this->meta=$metares;
	}
		import(APP_PATH.'/controller/lib/category.php');
		$ct=spClass("category");
		$result=$ct->findAll('','sid desc');
		$constructSm=new Tree($result);//construct sub menu
		$this->arr=$constructSm->leaf();
	$res['product_pic_1']=explode(',',$res['product_pic']);//0rginal
	$res['product_pic_2']=preg_replace('/(\d+)\//U','\1/thumb/',explode(',',$res['product_pic']));//thumb
	$this->res=$res;
	$this->display('management/product_add.html');


}else if($_REQUEST['act']&&$_REQUEST['act']=='add'){
	//$this->experts_options= $products;
/*-获取分类----------------------------*/	
		import(APP_PATH.'/controller/lib/category.php');
		$ct=spClass("category");
		$result=$ct->findAll('','sid desc');
		$constructSm=new Tree($result);//construct sub menu
		$this->arr=$constructSm->leaf();
		//$this->experts_options= $constructSm->leaf();	
			//$this->selected_id = 1;
/*-----------------------------------*/		
	$this->display('management/product_add.html');
}else if($_REQUEST['act']&&$_REQUEST['act']=='del'){//执行删除操作
		if(!$_REQUEST['id']){
			$this->error('必须指定需要操作产品的ID');
			die;
		}else{
			$id=intval($_REQUEST['id']);
			$ps=spClass(product_summary);
			$productDel=$ps->find($conditions=array('id'=>$id), $sort = null, $fields ='product_thumb',$limit='1');
			if(!$productDel){
				$this->error('指定记录不存在!');
			}else{
				$ps->delete($conditions=array('id'=>$id));
				@unlink (APP_PATH.$productDel['product_thumb']);
				@unlink (APP_PATH.preg_replace('/(\d+)\//U','\1/thumb/',$productDel['product_thumb']));	
				$this->success('恭喜，删除成功！');
			}
		}	
}else if($_REQUEST['display']){
			$id=intval($_REQUEST['id']);
			$ps=spClass(product_summary);
			$productDatilid=$ps->find($conditions=array('id'=>$id), $sort = null, $fields =null,$limit='1');
			if(!$productDatilid){
				$this->error('指定记录不存在!');
				die;
			}else{
				$display=$_REQUEST['display'];
				if($display=='true'){
					$ps->updateField($conditions=array(id=>$id),'product_status','1');
				}else if($display=='false'){
					$ps->update($conditions=array(id=>$id), $newrow=array('product_status'=>0,'show_order'=>0));
				}
			$this->success("恭喜！产品显示状态修改成功");					
			}
}			